package br.com.qualityempresa.core

import java.text.SimpleDateFormat

class DateUtils {

	static Date getDataHoraInicio(String dataParam, String formato) {
		def data 
		if (dataParam) {
			def formatDate = new SimpleDateFormat(formato)
			data = formatDate.parse(dataParam)
		} else {
			data = new Date()
		}
		return getDataHoraInicio(data)
	}
	
	static Date getDataHoraFim(String dataParam, String formato) {
		def data 
		if (dataParam) {
			def formatDate = new SimpleDateFormat(formato)
			data = formatDate.parse(dataParam)
		} else {
			data = new Date()
		}
		return getDataHoraFim(data)
	}

	static Date getDataHoraInicio(Date data) {
		if (!data) {
			data = new Date() 
		}
		def Calendar cal = Calendar.instance
		cal.setTimeInMillis(data.getMillisOf(data))
		cal.set(Calendar.AM_PM, Calendar.AM)
		cal.set(Calendar.HOUR, 0)
		cal.set(Calendar.MINUTE, 0)
		cal.set(Calendar.SECOND, 0)
		cal.set(Calendar.MILLISECOND, 0)
		data = cal.getTime()
		return data
	}

	static Date getDataHoraFim(Date data) {
		if (!data) {
			data = new Date()
		}
		def Calendar cal = Calendar.instance
		cal.setTimeInMillis(data.getMillisOf(data))
		cal.set(Calendar.AM_PM, Calendar.AM)
		cal.set(Calendar.HOUR, 23)
		cal.set(Calendar.MINUTE, 59)
		cal.set(Calendar.SECOND, 59)
		cal.set(Calendar.MILLISECOND, 999)
		data = cal.getTime()
		return data
	}
}
